package com.diandong.airfieldmanage.main.home.manageMemo;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.diandong.airfieldmanage.R;
import com.diandong.airfieldmanage.base.BaseActivity;
import com.diandong.airfieldmanage.main.home.manageMemo.adapter.ParentAdapter;
import com.diandong.airfieldmanage.main.home.manageMemo.bean.ChildEntity;
import com.diandong.airfieldmanage.main.home.manageMemo.bean.ParentEntity;
import com.diandong.airfieldmanage.main.home.manageMemo.bean.ThreeEntity;
import com.diandong.airfieldmanage.utils.ToastUtil;

import java.util.ArrayList;

/**
 * Created : Yx
 * Time : 2019/9/5 15:08
 * Describe :新增会议纪要，选人
 */
public class AddSelectUserActivity extends BaseActivity implements ExpandableListView.OnGroupExpandListener, ParentAdapter.OnChildTreeViewClickListener {
    private TextView tvLeft, tvTitle, tvRight;

    private ExpandableListView listView;
    private ArrayList<ParentEntity> parents;
    private ParentAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_meeting_seluser);
        tvLeft = findViewById(R.id.tv_left);
        tvTitle = findViewById(R.id.tv_title);
        tvRight = findViewById(R.id.tv_right);
        tvLeft.setOnClickListener(clicks);
        tvRight.setOnClickListener(clicks);
        listView = findViewById(R.id.add_meeting_select_user);
        listView.setOnGroupExpandListener(this);//分组展开的监听器
        adapter = new ParentAdapter(this);//父类分组适配器
        listView.setAdapter(adapter);
        adapter.setOnChildTreeViewClickListener(this);//设置条目点击事件（三级列表点击触发）
        loadData();//获取数据，假数据
        adapter.setData(parents);

    }

    /**
     * 初始化菜单数据源
     */
    private void loadData() {

        parents = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            ParentEntity parent = new ParentEntity(); //父类对象
            parent.setGroupName("父类父分组第" + i + "项");//假数据 ，设置一级列表数据

            ArrayList<ChildEntity> childs = new ArrayList<>();
            for (int j = 0; j < 4; j++) {
                ChildEntity child = new ChildEntity(); //子类对象
                child.setGroupName("子类父分组第" + j + "项");//假数据 ，二级列表数据

                ArrayList<ThreeEntity> childNames = new ArrayList<>();

                for (int k = 0; k < 5; k++) {
                    ThreeEntity threeEntity = new ThreeEntity();
                    threeEntity.setId(k + "");
                    threeEntity.setName("子类第" + k + "项");
                    childNames.add(threeEntity);//假数据 三级列表数据
                }
                child.setChildNames(childNames);
                childs.add(child);
            }
            parent.setChilds(childs);
            parents.add(parent);
        }
    }


    private View.OnClickListener clicks = v -> {
        switch (v.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                //发布
                break;
        }
    };


    /**
     * 点击子ExpandableListView的子项时，回调本方法，根据下标获取值来做相应的操作
     */
    @Override
    public void onClickPosition(int parentPosition, int groupPosition, int childPosition) {
        // do something
        String childName = parents.get(parentPosition).getChilds()
                .get(groupPosition).getChildNames().get(childPosition).getName()
                .toString();

        ToastUtil.showToastApplication("点击的下标为： parentPosition=" + parentPosition
                + "   groupPosition=" + groupPosition
                + "   childPosition=" + childPosition + "\n点击的是："
                + childName);
    }

    /**
     * 展开一项，关闭其他项，保证每次只能展开一项
     */
    @Override
    public void onGroupExpand(int groupPosition) {
        for (int i = 0; i < parents.size(); i++) {
            if (i != groupPosition) {
                //listView.collapseGroup(i);//关闭状态
            }
        }
    }


}
